package com.dayaokeji.rhythmschoolstudent.utils;

import android.util.SparseArray;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.List;
import java.util.Locale;

/**
 * 时间工具类
 */

public class DateUtils {

    public static final String FORMAT_DATE = "yyyy-MM-dd";
    public static final String FORMAT_DATE_SHORT = "MM-dd";
    public static final String FORMAT_TIME = "HH:mm:ss";
    public static final String FORMAT_TIME_SHORT = "HH:mm";
    public static final String FORMAT_DATETIME = "yyyy-MM-dd HH:mm:ss";
    public static final String FORMAT_DATETIME_SHORT = "MM-dd HH:mm";
    public static final String FORMAT_DATETIME_SHORT2 = "yyyy-MM-dd HH:mm";
    public static final String FORMAT_SMART = "{SMART}";

    public static final SparseArray<DateFormat> dateFormatMap;

    static {
        dateFormatMap = new SparseArray<>(7);
        dateFormatMap.put(FORMAT_DATE.hashCode(), new SimpleDateFormat(FORMAT_DATE, Locale.CHINA));
        dateFormatMap.put(FORMAT_DATE_SHORT.hashCode(), new SimpleDateFormat(FORMAT_DATE_SHORT, Locale.CHINA));
        dateFormatMap.put(FORMAT_TIME.hashCode(), new SimpleDateFormat(FORMAT_TIME, Locale.CHINA));
        dateFormatMap.put(FORMAT_TIME_SHORT.hashCode(), new SimpleDateFormat(FORMAT_TIME_SHORT, Locale.CHINA));
        dateFormatMap.put(FORMAT_DATETIME.hashCode(), new SimpleDateFormat(FORMAT_DATETIME, Locale.CHINA));
        dateFormatMap.put(FORMAT_DATETIME_SHORT.hashCode(), new SimpleDateFormat(FORMAT_DATETIME_SHORT, Locale.CHINA));
        dateFormatMap.put(FORMAT_DATETIME_SHORT2.hashCode(), new SimpleDateFormat(FORMAT_DATETIME_SHORT2, Locale.CHINA));
    }

    public static String format(long timeInMills, String format) {
        return format(new Date(timeInMills), format);
    }

    public static String format(Calendar calendar, String format) {
        return format(calendar.getTime(), format);
    }

    public static String format(Date date, String format) {
        if (FORMAT_SMART.equals(format)) return smartFormat(date);
        DateFormat dateFormat = dateFormatMap.get(format.hashCode(), new SimpleDateFormat(format));
        return dateFormat.format(date);
    }

    private static String smartFormat(Date date) {
        return format(date, FORMAT_DATETIME_SHORT);
    }

    /**
     * 字符转换时间戳
     * @param date
     * @return
     */
    public static Long formatTimestamp(String date) {
        DateFormat dateFormat = dateFormatMap.get(FORMAT_DATETIME.hashCode(), new SimpleDateFormat(FORMAT_DATETIME));
        try {
            Date parse = dateFormat.parse(date);
            return parse.getTime();
        } catch (ParseException e) {
            e.printStackTrace();
            return null;
        }
    }

    /**
     * 字符转换时间
     * @param date
     * @return
     */
    public static Date formatDate(String date,String format) {
        DateFormat dateFormat = dateFormatMap.get(format.hashCode(), new SimpleDateFormat(format));
        try {
            Date parse = dateFormat.parse(date);
            return parse;
        } catch (ParseException e) {
            e.printStackTrace();
            return null;
        }
    }

    /**
     * 获取当天的开始时间
     * @return
     */
   public static Date getDayBegin() {
       final Calendar cal = new GregorianCalendar();
       cal.set(Calendar.HOUR_OF_DAY, 0);
       cal.set(Calendar.MINUTE, 0);
       cal.set(Calendar.SECOND, 0);
       cal.set(Calendar.MILLISECOND, 0);
       return cal.getTime();
   }

    /**
     * 获取当天的结束时间
     * @return
     */
    public static Date getDayEnd() {
        Calendar cal = new GregorianCalendar();
        cal.set(Calendar.HOUR_OF_DAY, 23);
        cal.set(Calendar.MINUTE, 59);
        cal.set(Calendar.SECOND, 59);
        return cal.getTime();
     }

    /**
     * 获取明天的结束时间
     * @return
     */
     public static Date getEndDayOfTomorrow() {
        Calendar cal = new GregorianCalendar();
        cal.setTime(getDayEnd());
        cal.add(Calendar.DAY_OF_MONTH, 1);
        return cal.getTime();
     }

    // 获得本周一与当前日期相差的天数
    public static  int getMondayPlus() {
        Calendar cd = Calendar.getInstance();
        int dayOfWeek = cd.get(Calendar.DAY_OF_WEEK);
        if (dayOfWeek == 1) {
            return -6;
        } else {
            return 2 - dayOfWeek;
        }
    }

    /**
     * 获得当前周- 周一的日期
     * @return
     */
    public static  String getCurrentMonday() {
        int mondayPlus = getMondayPlus();
        GregorianCalendar currentDate = new GregorianCalendar();
        currentDate.add(GregorianCalendar.DATE, mondayPlus);
        Date monday = currentDate.getTime();
        String preMonday = format(monday,FORMAT_DATE);
        return preMonday+" 00:00:00";
    }


    /**
     * 获得当前周- 周日的日期
     * @return
     */
    public static String getCurrentSunday() {
        int mondayPlus = getMondayPlus();
        GregorianCalendar currentDate = new GregorianCalendar();
        currentDate.add(GregorianCalendar.DATE, mondayPlus +6);
        Date monday = currentDate.getTime();
        String preMonday = format(monday,FORMAT_DATE);
        return preMonday+" 23:59:59";
    }

    /**
     * 得到一个星期日期时间
     * @return
     */
    public static String[] getWeekDates() {
        final String[] weeks = new String[7];
        Calendar calendar = Calendar.getInstance();
        setToFirstDay(calendar);
        for (int i = 0; i < 7; i++) {
            weeks[i] = format(calendar.getTime(),FORMAT_DATE);
            calendar.add(Calendar.DATE, 1);
        }
        return weeks;
    }

    private static final int FIRST_DAY = Calendar.MONDAY;

    private static void setToFirstDay(Calendar calendar) {
        while (calendar.get(Calendar.DAY_OF_WEEK) != FIRST_DAY) {
            calendar.add(Calendar.DATE, -1);
        }
    }


    /**
     * 根据开始时间和结束时间返回时间段内的时间集合
     *
     * @param beginDate
     * @param endDate
     * @return List
     */
    public static List<String> getDates(Date beginDate, Date endDate) {
        List<String> lDate = new ArrayList<String>();
        lDate.add(format(beginDate.getTime(), FORMAT_DATE));// 把开始时间加入集合
        Calendar cal = Calendar.getInstance();
        // 使用给定的 Date 设置此 Calendar 的时间
        cal.setTime(beginDate);
        boolean bContinue = true;
        while (bContinue) {
            // 根据日历的规则，为给定的日历字段添加或减去指定的时间量
            cal.add(Calendar.DAY_OF_MONTH, 1);
            // 测试此日期是否在指定日期之后
            if (endDate.after(cal.getTime())) {
                lDate.add(format(cal.getTime(), FORMAT_DATE));
            } else {
                break;
            }
        }
        lDate.add(format(endDate.getTime(), FORMAT_DATE));// 把结束时间加入集合
        return lDate;
    }
}
